package com.ahccc.codegenerator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.sql.ResultSet;

/**
 * 字段元数据信息
 * @author ahccc_lk
 *
 */
public class MySqlDbColumn implements Serializable {
	private static final long serialVersionUID = -2789251413074297732L;
	protected Logger log = LoggerFactory.getLogger(getClass());
	// 字段名称
	private String code;
	// 备注名称
	private String name;
	// SQL 类别 特殊情况下用到
	private String sqlType;
	// 是否主键
	private boolean pk;
	// JAVA.SQL.TYPE
	private String javaType;
	// 字段长度
	private int length;
	/// 整数部分长度
	private int precision;
	// 小小部分长度
	private int scale;
	// 是否允许为空
	private boolean nullAble;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getjavaType() {
		return javaType;
	}

	public void setjavaType(String javaType) {
		this.javaType = javaType;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public boolean isNullAble() {
		return nullAble;
	}

	public void setNullAble(boolean nullAble) {
		this.nullAble = nullAble;
	}

	public MySqlDbColumn() {
	}

	public MySqlDbColumn(ResultSet rs) throws Exception {
		this.code = rs.getString("COLUMN_NAME");
		this.name = rs.getString("COLUMN_COMMENT");
		if (StringUtils.isEmpty(name))
			name = code;
		this.javaType =getJavaType(rs.getString("DATA_TYPE"));
		this.length = rs.getInt("CHARACTER_MAXIMUM_LENGTH");
		this.precision = rs.getInt("NUMERIC_PRECISION");
		this.scale = rs.getInt("NUMERIC_SCALE");
		this.nullAble = rs.getString("IS_NULLABLE").equals("NO")?false:true;
		this.sqlType = rs.getString("COLUMN_TYPE");

	}

	public String getJavaType(String sqlType) throws Exception {
		switch (sqlType) {
			case "longtext":
				return "String";
			case "text":
				return "String";
			case "varchar":
				return "String";
			case "char":
				return "String";
			case "longvarchar":
				return "String";
			case "blob":
				return "byte[]";
			case "int":
				return "Integer";
			case "integer":
				return "Integer";
			case "tinyint":
				return "Integer";
			case "smallint":
				return "Integer";
			case "mediumint":
				return "Integer";
			case "bit":
				return "Boolean";
			case "bigint":
				return "Long";
			case "double":
				return "Double";
			case "float":
				return "Float";
			case "numeric":
				return "java.math.BigDecimal";
			case "decimal":
				return "java.math.BigDecimal";
			case "date":
				return "java.util.Date";
			case "datetime":
				return "java.util.Date";
		default:
			throw new Exception(" 未知道的类别 " + sqlType);
		}
	}

	public String getFixCode() {
		String c = getSimpleName();
		return new StringBuffer().append(c.toUpperCase().substring(0, 1)).append(c.substring(1)).toString();
	}

	public String getSimpleName() {
		return getFixName(code);
	}

	public static String getFixName(String name) {
		String s = name.toLowerCase();
		StringBuffer sb = new StringBuffer();
		boolean need = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if ('_' == c) {
				need = true;
				continue;
			}
			if (need) {
				sb.append((char) (c - 32));
				need = false;
			} else
				sb.append(c);
		}
		return sb.toString();
	}

	public boolean isPk() {
		return pk;
	}

	public void setPk(boolean pk) {
		this.pk = pk;
	}

}
